import {Component, OnInit, ViewChild} from '@angular/core';
import {APARTMENT} from "../../service/apartment.service";
import {ActivatedRoute} from "@angular/router";
import {LocalStorage} from "../../app.local-storage";
import {ROUTE} from "../../service/route.service";
import {CONSTANT} from "../../service/constant.service";
import {LoginModalViewComponent} from "../login-modal-view/login-modal-view.component";
import {USER} from "../../service/user.service";
import {UserModel} from "../../model/user.model";

@Component({
  selector: 'app-renting-view',
  templateUrl: './renting-view.component.html',
  styleUrls: ['./renting-view.component.scss']
})
export class RentingViewComponent implements OnInit {

  progressBarList:any=this.constant.progressBarList;
  @ViewChild(LoginModalViewComponent) loginModal:any;
  user:UserModel;
  constructor(private apartment: APARTMENT,
              private route: ROUTE,
              private routeInfo:ActivatedRoute,
              private localStorage: LocalStorage,
              private constant:CONSTANT,
              private USER: USER) {
    this.user = this.USER.get();


  }
  apartmentId:number = undefined;
  apartmentList: any = [];
  data:any = null;
  buildingList: any = [];//楼栋列表
  currentBuildingId:number = 0;
  floorList:any = [];
  currentFloorId:any = 0;
  currentFloor:any = null;
  layoutList:any = [];
  currentLayoutId:any = 0;
  isHasHouse:boolean = false;
  roomData:any = [];
  selectLayoutRoomData:any=[];
  showLoginModal: boolean = false;
  selectedRoom:any = null;
  type:any = 1;

  // 可以获取公寓列表
  loadApartmentList() {
    let reqData = {
      page: 0,
      size: 99
    };
    this.apartment.getList(reqData, (res: any) => {
      this.apartmentList = res.content;
    })
  }
  //获取房型列表
  loadHouseList(type:any){
    let that:any = this;
    let data = {
      apartmentId:that.apartmentId,
      layoutId:type== 1 ?'':that.currentLayoutId,
      buildingId:that.currentBuildingId,
      floor:that.currentFloorId,
    }
    that.apartment.getHouseList(data,(res:any) =>{
      console.log(res);
        type == 1 ? that.roomData = res : that.selectLayoutRoomData = [];//
        if(type==2) {
          that.selectLayoutRoomData = res;
          that.deleAttr(that.roomData);
          for( let i= 0; i< that.roomData.length;i++){
            for(let j=0 ;j< that.selectLayoutRoomData.length; j++){
              if(that.selectLayoutRoomData[j].id==that.roomData[i].id){
                that.roomData[i].selected = true;
                break;
              }
            }
          }
        }
    })
  }
   deleAttr(arr){
    arr.forEach(function(v){
      delete v.selected;
    });
  }
  closeLogin(result:Object){
    if(result instanceof UserModel){
      this.user=result;
      this.USER.update(result);
    }
    this.showLoginModal=false;
  }
  getBuilding(){//公寓改变重新获取楼栋信息
    let apartmentId = this.apartmentId;
    this.buildSelectors(apartmentId);
  }
  // 下拉框数据初始化
  buildSelectors(apartmentId: number) {
    let reqData = {
      apartmentId: apartmentId,
    };
    this.apartment.getBuildingList(reqData, (res:any)=>{
      let allLayout:any = {'id':1,'name':'所有房型'};
      let layoutList = !!res.layoutVOs.length && !!res.layoutVOs && res.layoutVOs||[];//获取楼栋列表
      layoutList.unshift(allLayout);
      this.layoutList = layoutList;
      this.currentLayoutId = this.layoutList[0].id;
      this.data = res;
      this.buildingChange('',this.data);//楼栋信息改变
    })
  }
  buildingChange(id:any,res:any){
    let buildingList:any = [{'id' :1,name :'暂无楼栋信息'}];
    this.buildingList = !!res.buildingVOs && !! res.buildingVOs.length && res.buildingVOs.length !=0 ? res.buildingVOs : buildingList;//获取楼栋列表
    !!id && !!this.buildingList.length?
      this.currentBuildingId = id : this.currentBuildingId = this.buildingList[0].id;
    this.floorChange('',this.currentBuildingId,this.buildingList);//楼层信息改变
  }
  floorChange(currentFloorId:any,currentBuildingId:any,buildingList:any){
    this.type = 1;
    this.currentLayoutId = 1;
    let currentBuild:any = null;
    let floorList:any = [{'id':1,'name':'暂无楼层信息'}];
    let that:any = this;
    !!currentBuildingId && !!buildingList&& !!buildingList.length&&  !!buildingList[0].floorDesc && !!(function(){
       return  buildingList = buildingList.filter(function(v:any,i:any){
         return v.id == currentBuildingId;
       })
    })() ? (function(){that.floorList = JSON.parse(buildingList[0].floorDesc) ; that.isHasHouse=true})() :
      (function(){that.floorList = floorList ; that.isHasHouse=false;})();

    currentFloorId ? that.currentFloorId = currentFloorId : that.currentFloorId = this.floorList[0].id;
    that.floorList.forEach(function(v:any,i:any){
      if(v.id== that.currentFloorId){
        if(!!v.thumb && v.thumb.replace(/\s/g,'') != '房态图'){
          let imgSrc:string = v.thumb.replace(/\s/g,'');
          that.currentFloor = v;
          that.currentFloor.src = imgSrc + '?currentFloorId=' +v.id + '&currentBuildingId='+ that.currentBuildingId + '&layoutId=' + that.currentLayoutId;
        }else{
          that.currentFloor = {
            src : 'http://placehold.it/960x415'
          };
        }
        return false;
      }
    });
    this.loadHouseList(this.type);
    this.selectedRoom = null;
  }
  layoutChange(currentLayoutId:any,res:any){
    if(this.currentLayoutId !=1){
      this.type = 2;
      this.loadHouseList(this.type);
    }else{
      this.deleAttr(this.roomData);
    }
  }
  roomStyle(item:any){
    if(item.status==0) return false;
    let bgColor = item.status==1 ? 'white' : (item.status==2 ? '#b2b2b2' : (item.status==4 ? '#efef5d' : (item.status==3?'#b1d469':'#b3b3b3')));
    let fontColor = item.status==1? '#000' : (item.status==2 ? '#000' : (item.status==4 ? '#848484' : '#010100'));
    let borderColor =  '#000';
    item.selected ? fontColor = '#b3b3b3' : fontColor;
    return {
      "width" : item.width+'px',
      "height" : item.height+'px',
      "line-height":item.height+'px',
      "left" : item.left+'px',
      "top" : item.top+'px',
      "font-size" : 11+"px",
      "background-color":bgColor,
      "border":'1px solid '+borderColor,
      "color":fontColor
    }
  }
  selectRoom(item:any){
    console.log(item)
    if(item.status==2||item.status==4||item.status==8) return false;
    if(item.status==3){
      item.status=1;
      this.selectedRoom = null;
      return false;
    }
    if(item.status==1){
      for(let i = 0; i < this.roomData.length; i++){
        if(this.roomData[i].status == 3){
          this.roomData[i].status = 1;
          break;
        }
      }
      item.status = 3;
      this.selectedRoom = item;
      return false;
    }
  }
  // 进入下单页面
  goToPlaceOrderView() {
    // console.log(this.selectedRoom);
    // // return false;
    if(this.user==null){
      this.showLoginModal =true;
    }else {
      this.route.goPlaceOrderView(this.selectedRoom);
    }
  }
  ngOnInit() {
    this.routeInfo.params.subscribe((param:any)=>{
      this.apartmentId = param.id;
      this.getBuilding();
    });
    this.loadApartmentList();
  }
}
